$(function () {
    var inquiry_id = GetQueryString('inquiry_id');
    var state = GetQueryString("inquiry_status");
    var matcher_id = GetQueryString('matcher_id');
    var matching_name = GetQueryString('matching_name');
    var fix_price_id = GetQueryString('fix_price_id');
    var fix_price_name = getQueryString('fix_price_name')
    var receiver_province = GetQueryString('receiver_province');
    var platform = GetQueryString('platform');
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
       return null;
    };
    function getState(state) {
        if (state == '10' || state == '20') {
            return '待匹配';
        } else if (state == '30' || state == '40') {
            return '待定价';
        } else if (state == '50') {
            return '已报价';
        } else if (state == '60') {
            return '已退回';
        } else if (state == '70') {
            return '已撤销';
        }else if (state == '80') {
            return '已下单';
        }
        ;
    }

//获取操作人的姓名及ID
    /*var loginData=JSON.parse(web.readCookie("loginData"));
    var service_name = loginData.user.username;
    var service_id = loginData.user.name;*/
    var service_name = '1';
    var service_id = '1';
// 询价单状态
    $('#inquiry_status').text('询价单状态：' + getState(state));
// 商品基础信息
    var baseParams = {
        "async": true,
        "crossDomain": true,
        "url": root.xunjiadan.getProductInfo,
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        "data": {
            "inquiry_id": inquiry_id                             //询价单编号
        }
    }

    $.ajax(baseParams).done(function (result) {
        $('#inquiry_id').text(result.inquiry_id);
        $('#inquiry_name').text(result.inquiry_name);
        $('#created_time').text(result.created_time);
        $('#collect_address').text(result.receiver_province_name + '/' + result.receiver_city_name + '/' + result.receiver_area_name + '/' + result.detail_address + '/' + result.street_name);
        $('#linkman').text(result.linkman);
        $('#linkman_phone').text(result.linkman_phone);
        $('#customer_name').text(result.customer_name);
        $('#other_explain').text(result.other_explain);
        if(result.source==20){
        $('#platform').text("APP");
    }else {
        $('#platform').text("PC");
    }
        if(result.claim_arrival_time=="1000-01-01"){
            $('#timemin').val("");
        }else {
            $('#timemin').val(result.claim_arrival_time);
        }
        if(result.claim_purchase_time=="1000-01-01 00:00:00"){
            $('#timemax').val("");
        }else {
            $('#timemax').val(result.claim_purchase_time);
        }
    });
//备注
    var remarkParams = {
        "async": true,
        "crossDomain": true,
        "url": root.xunjiadan.getRemarksList,
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        "data": {
            "inquiry_id": inquiry_id                          //询价单编号
        }
    }

    $.ajax(remarkParams).done(function (result) {
        if (result.length > 0) {
            $('#remarkTable').show();
        } else {
            $('#remarkTable').hide();
        }
        var html = '';
        for (var i = 0; i < result.length; i++) {
            html += '<tr>'
                + '<td>' + result[i].service_name + '</td>'
                + '<td>' + result[i].result + '</td>'
                + '<td>' + result[i].create_time + '</td>'
                + '<td id="' + result[i].id + '"><a href="javascript:void(0)" class="deleteRemarks">删除</a></td>'
                + '</tr>'
        }
        ;
        $('#remarksList').empty().append(html);
    });
    //商品附件
    var fileParams = {
        "async": true,
        "crossDomain": true,
        "url": root.xunjiadan.getFileList,
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        "data": {
            "inquiry_id": inquiry_id                          //询价单编号
        }
    }

    $.ajax(fileParams).done(function (result) {
        var html = '';
        for (var i = 0; i < result.length; i++) {
            if (result[i].file_name == '') {
                var urlName = result[i].file_path;
                var imgUrl = urlName.substring(0, urlName.indexOf('?'));
                result[i].file_name = imgUrl.substring(imgUrl.lastIndexOf('/') + 1);
            }
            if (result[i].file_type == 'pcx' || result[i].file_type == 'emf ' || result[i].file_type == 'gif' || result[i].file_type == 'bmp ' || result[i].file_type == 'jpg' || result[i].file_type == 'tif' || result[i].file_type == 'jpeg ' || result[i].file_type == 'png' || result[i].file_type == 'rle') {
                text = "查看";
            } else {
                text = "下载";
            }
            var strarea = result[i].file_name ;
            var po = strarea.split(".");
            html += '<li>'
                + '<b>附件' + (i + 1) + ':</b>'
                + '<div style="margin-top: -20px;margin-left: 38px;height: 20px;position: relative;"><a href="' + root.xunjiadan.downLoadFromUrl + '?urlStr=' + encodeURIComponent(result[i].file_path) + '&fileName=' + encodeURIComponent(po[0]) + '">' + text + '</a></div>'
                + '<div class="bg-grey" style="margin-left: 65px;position: relative;top: -25px;">'
                + '<span>' + result[i].file_name + '</span>'
                + '</div></li>';
        }
        $('#fujian').empty().append(html);
    });
    //商品清单
    var productParams = {
        "async": true,
        "crossDomain": true,
        "url": root.xunjiadan.getProductList,
        "method": "POST",
        "headers": {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        "data": {
            "inquiry_id": inquiry_id                          //询价单编号
        }
    };

    $.ajax(productParams).done(function (data) {
        console.log(data);
        if (data) {
            var html = '';
            for (var i = 0; i < data.length; i++) {
                var pic = isProductError(data[i].product_depict,1);
                var desc = isProductError(data[i].product_depict);
                if (data[i].claim_purchase_time == '1000-01-01 00:00:00') {
                    var claim_purchase_time = '';
                } else {
                    var claim_purchase_time = data[i].claim_purchase_time;
                }
                if (data[i].claim_arrival_time == '1000-01-01 00:00:00') {
                    var claim_arrival_time = '';
                } else {
                    var claim_arrival_time = data[i].claim_arrival_time;
                }

                html += '<tr id="' + data[i].id + '" belong_inquiry_id="' + data[i].belong_inquiry_id + '">'
                    + '<td class="rfqg_td1">' + (i + 1) + '</td>'
                    + '<td class="xfqg_td2" sku_id="' + data[i].sku_id + '" spu_id="' + data[i].spu_id + '">' + data[i].sku_code + '</td>'
                    + '<td class="xfqg_td3">' + data[i].product_name + '</td>'
                    + '<td class="xfqg_td4">' + data[i].sku_name + '</td>'
                    + '<td class="xfqg_td5">' + data[i].product_classify + '</td>'
                    + '<td class="xfqg_td6">' + data[i].brand + '</td>'  //品牌
                    + '<td class="xfqg_td7">' + '<img style="width:24px;height:auto;" src="' + pic + '"/>' + desc + '</td>'
                    + '<td class="xfqg_td8">' + data[i].order_fillrates + '</td>'
                    + '<td class="xfqg_td10" >' + data[i].discount_price + '</td>'  //价格
                    + '<td class="xfqg_td11">' + data[i].product_count + '</td>'
                    + '<td class="xfqg_td12">' + data[i].unit_name + '</td>'
                    + '<td class="xfqg_td13">' + data[i].subtotal + '</td>'
           /*         + '<td class="xfqg_td14">' + claim_arrival_time + '</td>'
                    + '<td class="xfqg_td15" created_time="' + data[i].created_time + '" update_time="' + data[i].update_time + '">' + claim_purchase_time + '</td>'*/
                    + '</tr>'
            }
            ;
            $('#productList').empty().append(html);
        }
    });
    //删除备注
    $('#remarkTable').on('click', '.deleteRemarks', function () {
        if (!confirm('确定是否删除？')) {
            return false;
        }
        var Result_id = $(this).parent('td').attr('id');
        var ele = $(this);
        var settings6 = {
            "async": true,
            "crossDomain": true,
            "url": root.xunjiadan.deleteRemarks,
            "method": "POST",
            "headers": {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            "data": {
                "Result_id": Result_id
            }
        }

        $.ajax(settings6).done(function (response) {
            if (response.code == '0') {
                alert('删除成功！');
                ele.parents('tr').remove();
                if ($('#remarksList tr').length == 0) {
                    $('#remarkTable').hide();
                }
            } else {
                alert(response.message);
            }
        });
    });
});

//操作日志
function operationlog() {
    var inquiry_id = GetQueryString('inquiry_id');
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    };
    sessionStorage.setItem("inquiry_id",inquiry_id)
    var title="操作日志"
    var url="operationLog.html"
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}